package com.yunxi.service.crm.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yunxi.core.base.Constant;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.eai.pojo.crm.CustomerFollowVO;
import com.yunxi.eai.pojo.crm.CustomerIssuedVO;
import com.yunxi.eai.pojo.esb.EsbVO;
import com.yunxi.eai.service.crm.CrmService;
import com.yunxi.model.eai.EaiLog;
import com.yunxi.rocketmq.producer.EaiLogsProducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;

/**
 * Created by Administrator on 2019/7/11.
 */
@Service
@Qualifier("crmServiceImpl")
public class CrmServiceImpl implements CrmService {
    private static final Logger logger = LoggerFactory.getLogger(CrmServiceImpl.class);
    @Autowired
    private EaiLogsProducer eaiLogsProducer;
    @Override
    public ApiResult distributeCrm(EsbVO esbVO) {
        String serviceName = esbVO.getServiceName();
        // String reqBody = esbVO.getReqBody();
        ApiResult apiResult=null;
        if (serviceName.equals(Constant.CRM_PMS_CUSTOMER_LSSUED)) {
            //客户数据下发
            apiResult=saveCustomerLssued(esbVO);
        } else if (serviceName.equals(Constant.CRM_PMS_CUSTOMER_FOLLOW)) {
            //总部下发跟进
            apiResult=saveCustomerFollow(esbVO);
        }  else {
            return ResponseUtil.fail(500,"没有对应的业务接口!");
        }
        return apiResult;
    }

    private ApiResult saveCustomerFollow(EsbVO esbVO) {
        ApiResult apiResult = null;
        String reqBody = esbVO.getReqBody();
        JSONObject jsonObject = JSON.parseObject(reqBody);
        ArrayList<CustomerFollowVO> customerFollowList = JSON.parseObject(String.valueOf( jsonObject.get("data")), new TypeReference<ArrayList<CustomerFollowVO>>() {
        });
        //调用业务接口传递list
        EaiLog eaiLog=new EaiLog();
        eaiLog.setEsbEaiContent(reqBody);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("EAI系统发送客户跟踪数据给PMS系统");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(Constant.CRM_PMS_CUSTOMER_FOLLOW);
        eaiLog.setRequestId(esbVO.getRequestId());
        try {
            apiResult=ResponseUtil.ok();
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setPmsResultContent(JSONObject.toJSONString(apiResult));
            if (apiResult.getCode()==-1){
                eaiLog.setDisposeStatus(2);
            }
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            apiResult=ResponseUtil.fail();
            eaiLog.setDisposeStatus(2);
        }
        //  Message message = new Message(rocketConfig.getEaiLogTopic(), "eaiLog",(JSONObject.toJSONString(eaiLog)).getBytes() );
        try {
            //  SendResult send = eaiLogProducer.getProducer().send(message);
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
        return apiResult;
    }

    private ApiResult saveCustomerLssued(EsbVO esbVO) {
        ApiResult apiResult = null;
        String reqBody = esbVO.getReqBody();
        JSONObject jsonObject = JSON.parseObject(reqBody);
        ArrayList<CustomerIssuedVO> customerIssuedList = JSON.parseObject(String.valueOf( jsonObject.get("data")), new TypeReference<ArrayList<CustomerIssuedVO>>() {
        });
        //调用业务接口传递list
        EaiLog eaiLog=new EaiLog();
        eaiLog.setEsbEaiContent(reqBody);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("EAI系统发送客户数据给PMS系统");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(Constant.CRM_PMS_CUSTOMER_LSSUED);
        eaiLog.setRequestId(esbVO.getRequestId());
        try {
            apiResult=ResponseUtil.ok();
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setPmsResultContent(JSONObject.toJSONString(apiResult));
            if (apiResult.getCode()==-1){
                eaiLog.setDisposeStatus(2);
            }
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            apiResult=ResponseUtil.fail();
            eaiLog.setDisposeStatus(2);
        }
        //  Message message = new Message(rocketConfig.getEaiLogTopic(), "eaiLog",(JSONObject.toJSONString(eaiLog)).getBytes() );
        try {
            //  SendResult send = eaiLogProducer.getProducer().send(message);
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
        return apiResult;
    }
}
